package com.zs.backend.modules.dictionaryManagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zs.backend.common.utils.BeanUtil;
import com.zs.backend.common.utils.StringUtil;
import com.zs.backend.modules.dictionaryManagement.dto.DictionaryManagementAddOrUptRequest;
import com.zs.backend.modules.dictionaryManagement.dto.DictionaryManagementDTO;
import com.zs.backend.modules.dictionaryManagement.model.DictionaryManagement;
import com.zs.backend.modules.dictionaryManagement.mapper.DictionaryManagementMapper;
import com.zs.backend.modules.dictionaryManagement.service.DictionaryManagementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author zs
 * @since 2024-03-23
 */
@Service
public class DictionaryManagementServiceImpl extends ServiceImpl<DictionaryManagementMapper, DictionaryManagement> implements DictionaryManagementService {


    @Override
    public Page<DictionaryManagement> list(DictionaryManagementDTO dictionaryManagementDTO, Integer pageSize, Integer pageNum) {
        Page<DictionaryManagement> page = new Page<>(pageNum,pageSize);
        QueryWrapper<DictionaryManagement> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<DictionaryManagement> lambda = wrapper.lambda();
        lambda.likeRight(StringUtil.isNotEmpty(dictionaryManagementDTO.getDictionaryName()),
            DictionaryManagement::getDictionaryName, dictionaryManagementDTO.getDictionaryName());

        if(StringUtil.isNotEmpty(dictionaryManagementDTO.getTypes())){
            List<Integer> types = new ArrayList<>();
            if(dictionaryManagementDTO.getTypes().contains(",")){
                List<String> typeStrs = Arrays.asList(dictionaryManagementDTO.getTypes().split(","));
                types = typeStrs.stream().map(Integer::parseInt).collect(Collectors.toList());
            }else{
                types.add(Integer.parseInt(dictionaryManagementDTO.getTypes()));
            }
            lambda.in(DictionaryManagement::getType, types);
        }

        return page(page,wrapper);
    }

    @Override
    public boolean addOrUpt(DictionaryManagementAddOrUptRequest dictionaryManagementDTO) {

        DictionaryManagement dictionaryManagement = BeanUtil.beanCopyProperties(dictionaryManagementDTO, DictionaryManagement.class);

        if(dictionaryManagement.getId() != null){
            return updateById(dictionaryManagement);
        }
        return save(dictionaryManagement);
    }

    @Override
    public boolean updateDel(Long id) {
        DictionaryManagement dictionaryManagement = getById(id);
        return removeById(dictionaryManagement);
    }

}
